package com.bharatonjava.swift.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.bharatonjava.swift.domain.Employee;

public class EmployeeDao implements IEmployeeDao {

	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public Long addEmployee(final Employee employee) {
		
		KeyHolder keyHolder = new GeneratedKeyHolder();
		
		this.jdbcTemplate.update(new PreparedStatementCreator() {
			
			@Override
			public PreparedStatement createPreparedStatement(Connection c)
					throws SQLException {
				String sql = "INSERT INTO EMPLOYEE (EMPLOYEE_NAME, QUALIFICATION,SPELIALIZATION,PHONE_NUMBER,MOBILE_NUMBER,EMAIL,ADDRESS_ID,FEE_ID)" +
						" VALUES(?,?,?,?,?,?,?,?)";
				
				PreparedStatement ps = c.prepareStatement(sql, new String[]{"EMPLOYEE_ID"});
				ps.setString(1, employee.getEmployeeName());
				ps.setString(2, employee.getQualification());
				ps.setString(3, employee.getSpecialization());
				ps.setString(4, employee.getPhoneNumber());
				ps.setString(5, employee.getMobileNumber());
				ps.setString(6, employee.getEmail());
				ps.setLong(7, employee.getAddressId());
				ps.setLong(8, employee.getFeeId());
				return ps;
			}
		}, keyHolder);
		
		return keyHolder.getKey().longValue();

	}
}
